<?php
class Page_model_page extends Model {
    private $table_page  = 'page';
    private $table_image = 'n_image';
    
    public function add($node_id, $template) {
        $node_id = (int) $node_id;
        $template = (string) $template;
        $template = trim($template);
        
        if ($node_id <= 0) tos_die();
        if (empty($template)) tos_die();
        
        return $this->db->insert(
            $this->table_page, 
            array(
                'node_id' => $node_id,
                'template' => $template,
            ),
            array(
                'node_id' => DB_DATA_INT
            )
           );
    }
    
    
    public function edit_content($node_id, $data) {
        
        $node_id = (int) $node_id;
        $template = isset($data['template']) ? trim($data['template']) : '';
        
        if ($node_id <= 0) tos_die();
        
        
        $row = $this->db->select_row(
            $this->table_page,
            array('id'),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
        
        if ($row) {
            $id = (int) $row['id'];
            if ($id <= 0) tos_die();
            
            $delete_image_ids = isset($data['delete_image_ids']) ? 
                $data['delete_image_ids'] : array();
            if ($delete_image_ids) {
                $image_ids = array();
                foreach ($delete_image_ids as $image_id) {
                    $image_id = (int) $image_id;
                    if ($image_id > 0) {
                        $image_ids[] = $image_id;
                    }
                }
                if ($image_ids) {
                    $image_ids_sql = '(' . implode(',', $image_ids) . ')';
                    $sql = "DELETE FROM `{$this->table_image}` WHERE `id` in {$image_ids_sql}";
                    $this->db->execute($sql);
                }
            }
            
            if ($template) {
                $this->db->update(
                    $this->table_pate,
                    array('template' => $template),
                    array('node_id' => $node_id)
                );
            }
        } else {
            if (empty($template)) tos_die();
            $model_node = m('node')->model('node');
            $template_word = str_replace('_', ' ', $template);
            $model_node->add_words_to_node(array($template_word), $node_id, 1);
            
            $id = $this->db->insert(
                $this->table_page, 
                array(
                    'node_id' => $node_id,
                    'template' => $template,
                ),
                array(
                    'node_id' => DB_DATA_INT
                )
               );
        }
        if (isset($data['image'])) {
            $images = $data['image'];
            $this->_add_images($node_id, $images);
        }
        return $id;
        
    }
    
    
    public function get_content($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        $row = $this->db->select_row(
            $this->table_page,
            array('template'),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
        $images = $this->db->select_rows(
            $this->table_image,
            array(),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
        $row['images'] = $images;
        return $row;
    }
    
    
    public function remove_content($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        return $this->db->delete($this->table_page, array('node_id' => $node_id));
    }
    
    private function _add_images($node_id, $images) {
        if ( ! $images) return ;
        foreach ($images as $key => $image) {
            $this->db->insert(
                    $this->table_image, 
                    array(
                        'key' => $image['key'], 
                        'src' => $image['src'], 
                        'node_id' => $node_id,
                        'link' => $image['link']
                    )
                );
        }
    }
}
?>